# common
include $(TOP_DIR)/build/common.mk

# target
TARGET_STATIC = libldutils.a
TARGET_DYNAMIC = libldutils.so

# source file
DIRS := $(shell find . -maxdepth 3 -type d)
CFILES = $(foreach dir,$(DIRS),$(wildcard $(dir)/*.c))

# include file & path
INCLUDES = -I. -I./EasyLogger

# lib depended
LIBS =

# build params
ifeq ($(TARGET_OS) , android)
LIBS += -llog
else
LIBS += -pthread
endif

ifneq ($(LOG_FILE_NAME) , )
LOCAL_CFLAGS += -DELOG_FILE_NAME="\"$(LOG_FILE_NAME)\""
endif
ifneq ($(LOG_FILE_MAX_SIZE) , )
LOCAL_CFLAGS += -DELOG_FILE_MAX_SIZE=$(LOG_FILE_MAX_SIZE)
endif
ifneq ($(LOG_FILE_MAX_ROTATE) , )
LOCAL_CFLAGS += -DELOG_FILE_MAX_ROTATE=$(LOG_FILE_MAX_ROTATE)
endif

ifneq ($(USER_DATA_PATH) , )
LOCAL_CFLAGS += -DCFG_PATH="\"$(USER_DATA_PATH)\""
else
LOCAL_CFLAGS += -DCFG_PATH="\"/etc\""
endif

CCFLAGS = $(COMPILER_FLAGS) -c -fPIC $(LOCAL_CFLAGS)
CXXFLAGS = $(COMPILER_FLAGS) -c -fPIC -std=c++11 $(LOCAL_CFLAGS)

OBJFILE = $(CFILES:.c=.o) $(CXXFILES:.cpp=.o)

all:install

$(TARGET_STATIC): $(OBJFILE)
	$(AR) -cr $@ $(OBJFILE)

$(TARGET_DYNAMIC): $(OBJFILE)
	$(LINK) $^ $(LIBS) -Wall -fPIC -shared -o $@ -Wl,--allow-shlib-undefined -fpermissive

%.o:%.c
	$(CC) -o $@ $(CCFLAGS) $< $(INCLUDES)

%.o:%.cpp
	$(CXX) -o $@ $(CXXFLAGS) $< $(INCLUDES)

install:$(TARGET_STATIC) $(TARGET_DYNAMIC)
	$(INSTALL) $(TARGET_STATIC) $(OUT_DIR)/lib/
	$(INSTALL) $(TARGET_DYNAMIC) $(OUT_DIR)/lib/
	rm -rf $(TARGET_STATIC) $(TARGET_DYNAMIC) $(OBJFILE)

clean:
	rm -rf $(TARGET_STATIC) $(TARGET_DYNAMIC) $(OBJFILE)
	rm -rf $(OUT_DIR)/lib/$(TARGET_STATIC) $(OUT_DIR)/lib/$(TARGET_DYNAMIC)
